/* libfake437 - a library for the rendering of code page 437 `graphics'
 * Copyright (C) 2008, 2009, 2010 Jack Kelly <endgame.dos@gmail.com>
 *
 * This library is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"
#include <assert.h>
#include <SDL.h>
#include "fake437.h"
#include "fake437/display.hpp"

namespace fake437 {
  DisplayLayer::DisplayLayer(int left, int top, int width, int height):
    layer(f437_display_layer_new(left, top, width, height)) {}

  DisplayLayer::DisplayLayer(const DisplayLayer& other):
    layer(f437_display_layer_ref(other.layer)) {}

  DisplayLayer::DisplayLayer(F437DisplayLayer* layer):
    layer(layer) {}

  DisplayLayer::~DisplayLayer(void) {
    f437_display_layer_unref(layer);
  }

  DisplayLayer& DisplayLayer::operator =(const DisplayLayer& other) {
    if (&other == this) return *this;
    f437_display_layer_unref(this->layer);
    this->layer = f437_display_layer_ref(other.layer);
    return *this;
  }

  F437DisplayCell& DisplayLayer::get_cell(int x, int y) {
    assert(x >= 0 && x < this->layer->width);
    assert(y >= 0 && y < this->layer->height);
    return this->layer->cells[y * this->layer->height + x];
  }

  int DisplayLayer::get_left(void) const {
    return this->layer->left;
  }

  int DisplayLayer::get_top(void) const {
    return this->layer->top;
  }

  void DisplayLayer::set_left(int left) {
    this->layer->left = left;
  }

  void DisplayLayer::set_top(int top) {
    this->layer->top = top;
  }

  Display::Display(SDL_Surface* surface, const F437Font* font):
    display(f437_display_new(surface, font)) {}

  Display::~Display(void) {
    f437_display_free(this->display);
  }

  void Display::insert_layer(F437DisplayLayer* layer, int n) {
    f437_display_insert_layer(this->display, layer, n);
  }

  void Display::insert_layer(DisplayLayer& layer, int n) {
    this->insert_layer(layer.layer, n);
  }

  F437DisplayLayer* Display::get_layer(int n) {
    return f437_display_get_layer(this->display, n);
  }

  void Display::remove_layer(int n) {
    f437_display_remove_layer(this->display, n);
  }

  void Display::swap_layers(int n, int m) {
    f437_display_swap_layers(this->display, n, m);
  }

  void Display::push_layer(F437DisplayLayer* layer) {
    f437_display_push_layer(this->display, layer);
  }

  void Display::push_layer(DisplayLayer& layer) {
    this->push_layer(layer.layer);
  }

  F437DisplayLayer* Display::peek_layer(void) {
    return f437_display_peek_layer(this->display);
  }

  void Display::pop_layer(void) {
    f437_display_pop_layer(this->display);
  }

  void Display::draw(void) {
    f437_display_draw(this->display);
  }

  int Display::get_width(void) const {
    return this->display->surface->width;
  }

  int Display::get_height(void) const {
    return this->display->surface->height;
  }

  SDL_Surface* Display::get_surface(void) {
    return this->display->surface->surface;
  }
}
